/** EasyWeb iframe v3.1.4 date:2019-06-02 License By http://easyweb.vip */

// 以下代码是配置layui扩展模块的目录，每个页面都需要引入
layui.config({
    version: true,
    base: getProjectUrl() + 'assets/module/'
}).extend({
    formSelects: 'formSelects/formSelects-v4',
    treetable: 'treetable-lay/treetable',
    dropdown: 'dropdown/dropdown',
    notice: 'notice/notice',
    step: 'step-lay/step',
    dtree: 'dtree/dtree',
    citypicker: 'city-picker/city-picker',
    tableSelect: 'tableSelect/tableSelect',
    cropper: 'cropper/cropper',
    zTree: 'zTree/zTree',
    treeSelect: 'treeSelect/treeSelect',
    iconPicker: 'iconPicker/iconPicker',
    introJs: 'introJs/introJs',
    fileChoose: 'fileChoose/fileChoose',
    tagsInput: 'tagsInput/tagsInput',
    treeTable: 'treeTable/treeTable',
}).use(['layer', 'admin','table','form'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var admin = layui.admin;
    var table = layui.table;
    var form = layui.form;
    // 移除loading动画
    setTimeout(function () {
        admin.removeLoading();
    }, window == top ? 600 : 100);

    // 修改状态
    form.on('switch(updateState)', function (obj) {
        var ajaxUrl = $(obj.elem).attr("ajax-url");
        var ajaxState = $(obj.elem).attr("ajax-state");
        ajaxState || (ajaxState = 'state');
        var param={ids:obj.elem.value};
        if (!ajaxUrl) {
            B.error("缺少ajax-url属性");
            return;
        }
        // 区别其他状态，只有state状态是0为启用，1位禁用
        if(ajaxState=='state'){
            param[ajaxState]=obj.elem.checked ? 0 : 1;
        }else{
            param[ajaxState]=obj.elem.checked ? 1 : 0;
        }

        B.post({
            url: ajaxUrl,
            loading: true,
            data: param,
            success: function (data) {
                if (data.success) {
                    layer.msg(data.msg, {icon: 1});
                } else {
                    layer.msg(data.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }

        });

    });

    /** 所有ajax-event处理 */
    $(document).on('click', '*[ajax-event]',function () {
        var event = $(this).attr('ajax-event');
        var url = $(this).attr('ajax-url');
        var tableId = $('table[lay-filter]').attr('id');
        var _table = layui.table;
        switch(event){
            case 'batchDel':
                var checkStatus = _table.checkStatus(tableId);
                var data = checkStatus.data;
                var ids = new Array();
                var idName = $(this).attr('ajax-id');
                idName || (idName = 'id')
                if(data.length>0){
                    for (var i in data) {
                        ids.push(data[i][idName]);
                    }
                    layer.confirm('确定删除选中的数据？', {icon: 3, title: '提示信息',skin:'layui-layer-admin'}, function (index) {
                        B.del({
                            url:url+ids.join(","),
                            loading:true,
                            success:function(res){
                                if (res.success) {
                                    B.info(res.msg);
                                    //ins1.reload({},'data');
                                    _table.reload(tableId);
                                }else{
                                    B.error(res.msg);
                                }
                            }
                        })
                        layer.close(index);
                    })
                }else {
                    B.error("请选择需要删除的数据");
                }
                break;
            case 'clearAll':
                layer.confirm('确定清空所有数据吗？', {icon: 3, title: '提示信息',skin:'layui-layer-admin'}, function (index) {
                    B.del({
                        url:url,
                        loading:true,
                        success:function(res){
                            if (res.success) {
                                B.info(res.msg);
                                _table.reload(tableId);
                            }else{
                                B.error(res.msg);
                            }
                        }
                    })
                    layer.close(index);
                })
                break;
            case 'add':
                // 添加
                break;
            case 'del':
                // 删除
                var id = $(this).attr("value");
                if (B.isEmpty(id)) {
                    B.error('ID参数错误');
                }
                layer.confirm('确定删除数据吗？', {icon: 3, title: '提示信息',skin:'layui-layer-admin'}, function (index) {
                    B.del({
                        url:url+id,
                        loading:true,
                        success:function(res){
                            if (res.success) {
                                B.info(res.msg);
                                //ins1.reload({},'data');
                                _table.reload(tableId);
                            }else{
                                B.error(res.msg);
                            }
                        }
                    })
                    layer.close(index);
                });
                break;
            case 'export':
                // 导出数据
                var checkRows = _table.checkStatus(tableId);
                if (checkRows.data.length == 0) {
                    layer.msg('请选择要导出的数据', {icon: 2});
                } else {
                    _table.exportFile(tableId, checkRows.data, 'xls');
                }
                break;
        };

    });
});

// 获取当前项目的根路径，通过获取layui.js全路径截取assets之前的地址
function getProjectUrl() {
    var layuiDir = layui.cache.dir;
    if (!layuiDir) {
        var js = document.scripts, last = js.length - 1, src;
        for (var i = last; i > 0; i--) {
            if (js[i].readyState === 'interactive') {
                src = js[i].src;
                break;
            }
        }
        var jsPath = src || js[last].src;
        layuiDir = jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
    }
    return layuiDir.substring(0, layuiDir.indexOf('assets'));
}
